package com.example.mywood;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import bean.ContentDataBean;
import myapp.MyApp;
import util.MyLog;
import util.PreferenceUtil;
import util.SystemStatusManager;

public class GongShiActivity extends BaseActivity {


    private MyApp myapp;
    private Context context;
    private String token;
    private String z_about;
    private String e_about;
    private String j_about;
    private String domain;
    private String yuyan = "";
    private String apptype = "";
    private String z_formula;
    private String url = "";
    private SharedPreferences sharedPreferences;
    private LinearLayout ll_reback;
    private TextView tv_gongshi;
    private String textData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_gongshi);
        context = this;
        myapp = (MyApp) getApplicationContext();
        sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        token = sharedPreferences.getString("token", "");
        apptype = sharedPreferences.getString("apptype", "0");
        z_about = sharedPreferences.getString("z_about", "");
        e_about = sharedPreferences.getString("e_about", "");
        j_about = sharedPreferences.getString("j_about", "");
        domain = sharedPreferences.getString("domain", "");
        z_formula = sharedPreferences.getString("z_formula", "");
        textData = sharedPreferences.getString("textData", "");
        yuyan = PreferenceUtil.getString("language", "zh");
        url = getIntent().getStringExtra("url");
        setTranslucentStatus();
        initView();
        initLin();

    }
    @SuppressLint("JavascriptInterface")
    private void initView() {

        tv_gongshi = findViewById(R.id.tv_gongshi);

        final ContentDataBean.DataBean textContent = new Gson().fromJson(textData, new TypeToken<ContentDataBean.DataBean>() {
        }.getType());
        if (textContent != null) {
            tv_gongshi.setText(textContent.getGongshibaojia());
        }

        ll_reback = findViewById(R.id.ll_reback);
        View view_back = findViewById(R.id.view_back);
        view_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        WebView webview = findViewById(R.id.webview);
        MyLog.e("666666666",domain+z_formula+"     0000000000");
        MyLog.e("666666666",url+"     0000000000");
        WebSettings webSettings = webview.getSettings(); //支持缩放，默认为true。
        webSettings.setUseWideViewPort(true); // 缩放至屏幕的大小 webSettings
        webSettings.setLoadWithOverviewMode(true); //设置默认编码
        webSettings .setDefaultTextEncodingName("utf-8"); ////设置自动加载图片
        webSettings .setLoadsImagesAutomatically(true);
        webSettings.setJavaScriptEnabled(true);// 设置可以运行JS脚本
        webSettings.setSupportZoom(false);// 用于设置webview放大
        webSettings.setBuiltInZoomControls(false);

        //支持js
        webSettings.setJavaScriptEnabled(true);
        //设置本地调用对象及其接口
        webview.addJavascriptInterface(new Waimairenjsobj(), "h5_android");
        webview.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //在这里执行你想调用的js函数

            }

            @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
            super.onReceivedSslError(view, handler, error);
        }
        });

        MyLog.e("公式url","https://"+url);
        webview.loadUrl("http://"+url);

    }

    private final class Waimairenjsobj {
        // 打电话
        @JavascriptInterface
        public void test() {
            MyLog.e("返回按钮","进入返回按钮");
            finish();
        }

    }
    public static void hideKeyboard(View view){
        InputMethodManager imm = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(),0);
        }
    }
    private void initLin() {
        ll_reback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideKeyboard(ll_reback);
                finish();
            }
        });
    }
    /**
     * 设置状态栏背景状态
     */
    public  void setTranslucentStatus()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            Window win =  getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            winParams.flags |= bits;
            win.setAttributes(winParams);
        }
        SystemStatusManager tintManager = new SystemStatusManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(0);//状态栏无背景
    }

}
